<?php
namespace common\service;

use Yii;

class MessageService {

    /**
     * 后台注册成功发送邮件
     * @param $account
     * @param $pwd
     * @param $toEmail
     * @return array
     */
    public static function sendBackendRegisterSuc($account,$pwd,$toEmail,$gooleKey=''){
        if(empty($account)){
            return ["code"=>400,"msg"=>"账号不能为空"] ;
        }
        if(empty($pwd)){
            return ["code"=>400,"msg"=>"密码不能为空"] ;
        }
        if(empty($toEmail)){
            return ["code"=>400,"msg"=>"邮件地址不能为空"] ;
        }

        $baseUrl = Yii::$app->params['message']['host']  ;
        $url = "{$baseUrl}sendMessageBus" ;
        $header = "Content-Type: application/json; charset=utf-8" ;
        $requestId = BaseToolService::createGuid() ;
        $appUserName = Yii::$app->params['message']['appId'] ;
        $key = Yii::$app->params['message']['key'] ;
        $batchId = BaseToolService::createGuid() ;
        $secert = "appUserName:{$appUserName}_requestId:{$requestId}_key:{$key}" ;
        $secert = md5($secert);
        //1:pc 2:安卓 3:IOS 4:wap 5:微信 6:后台触发
        $platformSource  = 6 ;
        $templateCode = "TD-PLUS-REGISTER-SUCC" ;
        $backend_url = Yii::$app->params['backend_url'] ;
        $params = [
            "url"       => $backend_url ,
            "account"   => $account ,
            "pwd"       => $pwd ,
            "goolekey" => $gooleKey ,
        ] ;
        $userList = [["emailAddress"=>$toEmail]] ;
        $data = [
            "appUserName" => $appUserName ,
            "batchId" => $batchId ,
            "key" => $secert ,
            "platformSource" => $platformSource ,
            "requestId" => $requestId ,
            "templateCode" => $templateCode ,
            "params" => $params ,
            "userDTOList" => $userList ,
        ] ;
        $curlArr = HttpService::curlPostDetail($url,$data,5,$header,'json') ;

        if(empty($curlArr['result'])){
            $errArr = [
                "inputData"  => ["url"    => $url ,"data"=>$data] ,
                "outputData" => ["result" => null]
            ] ;
            BaseToolService::error($errArr,"MessageService_sendBackendRegisterSuc_network_error") ;
            return ["code"=>503,"msg"=>"网络异常"] ;
        }
        $result = empty($curlArr['result'])?[]:$curlArr['result'] ;
        $rsArr = json_decode($result,true) ;
        if(empty($rsArr['code']) || $rsArr['code']!=200){
            $errArr = [
                "inputData"  => ["url"    => $url ,"data"=>$data] ,
                "outputData" => ["result" => $rsArr]
            ] ;
            BaseToolService::error($errArr,"MessageService_sendBackendRegisterSuc_error") ;
            return ["code"=>400,"msg"=>empty($rsArr['message'])?"":$rsArr['message']] ;
        }
        return ["code"=>200,"msg"=>"成功"] ;
    }

    /**
     * 发送重置密码邮件
     * @param $pwd
     * @param $toEmail
     * @return array
     */
    public static function sendBackendResetPwd($pwd,$toEmail){
        if(empty($pwd)){
            return ["code"=>400,"msg"=>"密码不能为空"] ;
        }
        if(empty($toEmail)){
            return ["code"=>400,"msg"=>"邮件地址不能为空"] ;
        }
        $baseUrl = Yii::$app->params['message']['host']  ;
        $url = "{$baseUrl}sendMessageBus" ;
        $header = "Content-Type: application/json; charset=utf-8" ;
        $requestId = BaseToolService::createGuid() ;
        $appUserName = Yii::$app->params['message']['appId'] ;
        $key = Yii::$app->params['message']['key'] ;
        $batchId = BaseToolService::createGuid() ;
        $secert = "appUserName:{$appUserName}_requestId:{$requestId}_key:{$key}" ;
        $secert = md5($secert);
        //1:pc 2:安卓 3:IOS 4:wap 5:微信 6:后台触发
        $platformSource  = 6 ;
        $templateCode = "TD-PLUS-RESET_PWD" ;
        $params = [
            "pwd"     => $pwd ,
        ] ;
        $userList = [["emailAddress"=>$toEmail]] ;
        $data = [
            "appUserName" => $appUserName ,
            "batchId" => $batchId ,
            "key" => $secert ,
            "platformSource" => $platformSource ,
            "requestId" => $requestId ,
            "templateCode" => $templateCode ,
            "params" => $params ,
            "userDTOList" => $userList ,
        ] ;
        $curlArr = HttpService::curlPostDetail($url,$data,5,$header,'json') ;

        if(empty($curlArr['result'])){
            $errArr = [
                "inputData"  => ["url"    => $url ,"data"=>$data] ,
                "outputData" => ["result" => null]
            ] ;
            BaseToolService::error($errArr,"MessageService_sendBackendResetPwd_network_error") ;
            return ["code"=>503,"msg"=>"网络异常"] ;
        }
        $result = empty($curlArr['result'])?[]:$curlArr['result'] ;
        $rsArr = json_decode($result,true) ;
        if(empty($rsArr['code']) || $rsArr['code']!=200){
            $errArr = [
                "inputData"  => ["url"    => $url ,"data"=>$data] ,
                "outputData" => ["result" => $rsArr]
            ] ;
            BaseToolService::error($errArr,"MessageService_sendBackendResetPwd_error") ;
            return ["code"=>400,"msg"=>empty($rsArr['message'])?"":$rsArr['message']] ;
        }
        return ["code"=>200,"msg"=>"成功"] ;
    }

    /**
     * 发送退款成功短信
     * @param $account
     * @param $orderId
     * @param $amount
     * @param $tuanbi
     * @param $tel
     * @param $toMobile
     * @return array
     */
    public static function sendRefund($account,$orderId,$amount,$tuanbi,$tel,$toMobile){
        if(empty($account)){
            return ["code"=>400,"msg"=>"账号不能为空"] ;
        }
        if(empty($orderId)){
            return ["code"=>400,"msg"=>"订单号不能为空"] ;
        }
        if(empty($amount)){
            return ["code"=>400,"msg"=>"金额不能为空"] ;
        }
        if(empty($toMobile)){
            return ["code"=>400,"msg"=>"手机号不能为空"] ;
        }
        $tuanbi = empty($tuanbi)?0:$tuanbi;
        $baseUrl = Yii::$app->params['message']['host']  ;
        $url = "{$baseUrl}sendMessageBus" ;
        $header = "Content-Type: application/json; charset=utf-8" ;
        $requestId = BaseToolService::createGuid() ;
        $appUserName = Yii::$app->params['message']['appId'] ;
        $key = Yii::$app->params['message']['key'] ;
        $batchId = BaseToolService::createGuid() ;
        $secert = "appUserName:{$appUserName}_requestId:{$requestId}_key:{$key}" ;
        $secert = md5($secert);
        //1:pc 2:安卓 3:IOS 4:wap 5:微信 6:后台触发
        $platformSource  = 4 ;
        $templateCode = "TD-PLUS-CANCEL_ORDER" ;
        $params = [
            "mobile"      => $account ,
            "orderId"     => $orderId ,
            "amount"      => $amount ,
            "tuanbi"      => $tuanbi ,
            "tel"         => $tel ,
        ] ;
        $userList = [["mobile"=>$toMobile]] ;
        $data = [
            "appUserName" => $appUserName ,
            "batchId" => $batchId ,
            "key" => $secert ,
            "platformSource" => $platformSource ,
            "requestId" => $requestId ,
            "templateCode" => $templateCode ,
            "params" => $params ,
            "userDTOList" => $userList ,
        ] ;
        $curlArr = HttpService::curlPostDetail($url,$data,5,$header,'json') ;

        if(empty($curlArr['result'])){
            $errArr = [
                "inputData"  => ["url"    => $url ,"data"=>$data] ,
                "outputData" => ["result" => null]
            ] ;
            BaseToolService::error($errArr,"MessageService_sendRefund_network_error") ;
            return ["code"=>503,"msg"=>"网络异常"] ;
        }
        $result = empty($curlArr['result'])?[]:$curlArr['result'] ;
        $rsArr = json_decode($result,true) ;
        if(empty($rsArr['code']) || $rsArr['code']!=200){
            $errArr = [
                "inputData"  => ["url"    => $url ,"data"=>$data] ,
                "outputData" => ["result" => $rsArr]
            ] ;
            BaseToolService::error($errArr,"MessageService_sendRefund_error") ;
            return ["code"=>400,"msg"=>empty($rsArr['message'])?"":$rsArr['message']] ;
        }
        return ["code"=>200,"msg"=>"成功"] ;
    }

}